<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <search
      :statusOptions="statusOptions"
      :btnFalse="btnFalse"
      :query="query"
      @queryData="queryData"
      @add="add"
      @reload="reload"
      :labelArr="labelArr"
    />
    <!-- 数据列表 :data 绑定渲染的数据, border 纵向边框 -->
    <el-table :data="list" border highlight-current-row style="width: 100%">
      <!-- type="index"获取索引值，从1开始 ，label显示标题，prop 数据字段名，width列宽 -->
      <el-table-column
        align="center"
        type="index"
        label="序号"
        width="60"
      ></el-table-column>
      <el-table-column align="center" prop="title" label="广告标题">
      </el-table-column>
      <el-table-column align="center" label="广告图片">
        <template slot-scope="scope">
          <!-- :preview-src-list 放大后的图片url，是数组，不要少中括号[] -->
          <el-image
            :src="scope.row.imageUrl"
            style="width: 90px; height: 60px"
            :preview-src-list="[scope.row.imageUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="advertUrl" label="广告链接"></el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="danger">禁用</el-tag>
          <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除
          </el-button>
        </template></el-table-column
      >
    </el-table>
    <!-- 分页 -->
    <pageination
      :currentPage="page.current"
      :pageSizeInfo="page.size"
      :total="page.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 弹窗 -->
    <audit
      :isAudit="audit.isAudit"
      :title="audit.title"
      :dialogVisible="audit.dialogVisible"
      :formData="audit.formData"
      :remoteForm="remoteForm"
      :oldImageUrl="audit.oldImageUrl"
    />
  </div>
</template>

<script>
import { ApiGetAdvertList, ApiGetAdvertId, ApiDeleteAdvertId } from '@/api/advert'
import pageination from '@/components/common/pageination.vue'
import search from '@/components/common/header-search.vue'
import audit from './components/audit.vue'
// 用于下拉框
const statusOptions = [
  { value: "0", label: '禁用' },
  { value: "1", label: '正常' }
]
export default {
  name: 'Advert',
  components: {
    pageination,
    search,
    audit
  },
  data () {
    return {
      list: [],
      labelArr: ['广告标题', '状态'],
      page: {
        current: 1,
        size: 20,
        total: 0
      },
      query: {},
      statusOptions,
      btnFalse: true, // 控制按钮显示隐藏
      audit: {
        dialogVisible: false,
        title: '',
        formData: {
          imageUrl: null // 不然上传新图片不可立刻预览
        },
        oldImageUrl: null // 编辑时的图片
      }
    }
  },
  methods: {
    async fetchData () {
      const { data: res } = await ApiGetAdvertList(this.query, this.page.current, this.page.size)
      this.list = res.records
      this.page.total = res.total
    },
    add () { // 新增
      this.audit.title = '新增'
      this.audit.dialogVisible = true
    },
    async handleEdit (id) { // 编辑操作
      const res = await ApiGetAdvertId(id)
      if (res.code === 20000) {
        // 将数据赋值回去 并保存旧图片地址 没更新图片就用旧图片
        this.audit.formData = res.data
        this.audit.oldImageUrl = res.data.imageUrl
        this.audit.title = '编辑'
        this.audit.dialogVisible = true
      }
    },
    openAudit (id) { // 审核文章
      this.audit.id = id
      this.audit.title = '审核文章'
      this.audit.isAudit = true
      this.audit.dialogVisible = true
    },
    openView (id) {// 查看文章
      this.audit.id = id
      this.audit.title = '文章详情'
      this.audit.isAudit = false
      this.audit.dialogVisible = true
    },
    remoteForm () { // 关闭弹窗
      this.audit.formData = { imageUrl: null }
      this.audit.dialogVisible = false
      this.fetchData()
    },
    searchForm () {
      this.fetchData()
    },
    async handleDelete (id) { // 删除文章
      this.$confirm('确认删除文章吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await ApiDeleteAdvertId(id)
        this.$message({
          type: res.code === 20000 ? 'success' : 'warn',
          message: res.message
        });
        // 刷新列表
        this.fetchData()
      }).catch(() => {

      });
    },
    queryData () { // 文章查询
      this.page.current = 1
      this.fetchData()
    },
    reload () { // 重置
      this.query = {}
      this.fetchData()
    },
    // 当前条数
    handleSizeChange (val) {
      this.page.size = val
      this.fetchData()
    },
    // 当前页数
    handleCurrentChange (val) {
      this.page.current = val
      this.fetchData()
    },
  },
  created () {
    this.fetchData()
  },
}
</script>

<style>
</style>